﻿using System;
using System.Xml;
using DotNetAgent.Core;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;

namespace DotNetAgent
{
	class DotNetAgentApp
	{
		protected static string LogFilename;
		//public static string LogFilePath = Assembly.GetExecutingAssembly().Location.Remove(Assembly.GetExecutingAssembly().Location.LastIndexOf(@"\") + 1);

		static void Main()
		{
			try
			{
				LogFilename = "logs\\" + "DotNetAgentApp" + "-" + DateTime.Now.ToString("yyyyMMdd") + ".log";
								
				Agent MainAgent = new Agent(new Dictionary<String, Object>()
				{
					{"Name","DotNetAgent1"}
					//{"LogFilename","logs\\DotNetAgent1.log"}
				});

				MainAgent.CreateAgent(new Dictionary<String, Object>()
				{
					{"Name",	"PingAgent1"},
					{"Type",	typeof(PingAgent).AssemblyQualifiedName},
					{ "Port", "8004" }, 
					{ "TargetAgent", "PingAgent2" }
				});

				MainAgent.CreateAgent(new Dictionary<String, Object>()
				{
					{"Name",	"PingAgent2"},
					{"Type",	typeof(PingAgent).AssemblyQualifiedName},
					{ "Port", "8003" }, 
					{ "TargetAgent", "PingAgent1" } 
				});

				MainAgent.Start();

				Agent.Sleep(10000);

				MainAgent.Stop(true,false);
				//while (MainAgent.RunStatus != AgentRunStatus.Idle)
				//{
				//    AgentHelper.Sleep(1000);
				//}

				MainAgent.Dispose();
				//while (MainAgent.RunStatus != AgentRunStatus.DeActivated)
				//{
				//    AgentHelper.Sleep(1000);
				//}

			}
			catch (Exception Ex)
			{
				System.Console.WriteLine(AgentException.FormatExceptionString(Ex));
			}
			finally
			{
				System.Console.WriteLine("Press any key to continue...");
				System.Console.ReadLine();
			}

		}
		public void Log(String Source, DateTime TimeStamp, string OperationName, string Category, string Msg)
		{
			WriteLogFile(Environment.NewLine + TimeStamp.ToString("yyyy-MM-dd HH:mm:ss.fff").PadRight(24, ' ') + "|" + Category.PadRight(13, ' ') + "|" + Source.PadRight(42, ' ') + "|" + OperationName.PadRight(32, ' ') + "|" + Msg.PadRight(15, ' '));
		}

		public static string GetLogFilepath()
		{
			return Assembly.GetExecutingAssembly().Location.Remove(Assembly.GetExecutingAssembly().Location.LastIndexOf(@"\") + 1);
		}
		
		public void WriteLogEnd()
		{
			try
			{
				WriteLogFile(Environment.NewLine + "".PadRight(24, '-') + "|" + "".PadRight(13, '-') + "|" + "".PadRight(42, '-') + "|" + "".PadRight(32, '-') + "|" + "".PadRight(79, '-'));
			}
			catch (Exception Ex)
			{
				throw new AgentException("WriteLogEnd", "Error During Log Writing End", Ex);
			}
		}

		protected void WriteLogFile(string Msg)
		{
			try
			{
				for (int retries = 0; retries < 10; retries++)
				{
					try
					{
						File.AppendAllText(LogFilename, Msg, Encoding.UTF8);
						break;
					}
					catch (Exception)
					{
						Agent.Sleep(100 * retries);
					}
				}
			}
			catch (Exception _Ex)
			{
				throw new AgentException("WriteLog", "Error During Writer Log Writing", _Ex);
			}
		}

		public void WriteLogStart()
		{
			try
			{
				WriteLogFile(Environment.NewLine + "-TIMESTAMP".PadRight(24, '-') + "|-CATEGORY".PadRight(14, '-') + "|-SOURCE".PadRight(43, '-') + "|-OPERATION".PadRight(33, '-') + "|-MESSAGE [PROPERTIES]".PadRight(80, '-'));
				WriteLogFile(Environment.NewLine + "".PadRight(24, '-') + "|" + "".PadRight(13, '-') + "|" + "".PadRight(42, '-') + "|" + "".PadRight(32, '-') + "|" + "".PadRight(79, '-'));
			}
			catch (Exception Ex)
			{
				throw new AgentException("WriteLogStart", "Error During Log Writing Start", Ex);
			}
		}
		public static void DebugLog(string Msg)
		{
			try
			{
				File.AppendAllText(GetLogFilepath() + "DotNetAgent-debug.log", Environment.NewLine + "[" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "]\t" + Msg, Encoding.UTF8);
			}
			catch (Exception)
			{
			}
		}

		public static void DebugLog(string Msg, Exception Ex)
		{
			DebugLog(Msg + ": " + AgentException.FormatExceptionString(Ex));
		}
	}

}
